package com.simtop.beefsheepmaching.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.simtop.beefsheepmaching.common.ServerResponse;
import com.simtop.beefsheepmaching.common.UserUtil;
import com.simtop.beefsheepmaching.pojo.experiment.Experiment;
import com.simtop.beefsheepmaching.pojo.experiment.ExperimentReportVo;
import com.simtop.beefsheepmaching.service.ExperimentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author lvyongwei
 */
@RestController
@RequestMapping("/experiment")
public class ExperimentController {

    @Autowired
    private ExperimentService experimentService;

    /**
     * 提交成绩
     * @param experiment
     * @param request
     * @return
     */
    @PostMapping("/add")
    public ServerResponse add(@RequestBody Experiment experiment, HttpServletRequest request){
        try{
            experiment.setUserId(UserUtil.getUserId(request));
            boolean flag = experimentService.insertExperiment(experiment);
            if(flag==false){
                return ServerResponse.createByErrorMsg("提交内容失败");
            }
            return ServerResponse.createBySuccessMsg("提交内容成功");
        } catch (Exception e){
            e.printStackTrace();
            return ServerResponse.createByErrorMsg("提交内容失败");
        }
    }

    /**
     * 修改提交内容
     * @param experiment
     * @return
     */
    @PostMapping("/update")
    public ServerResponse update(@RequestBody Experiment experiment){
        try{
            experimentService.updateExperiment(experiment);
            return ServerResponse.createBySuccessMsg("修改成绩成功");
        }catch (Exception e){
            return ServerResponse.createByErrorMsg("修改成绩失败");
        }
    }

    /**
     * 根据id删除实验报告
     */
    @RequestMapping(value = "/deletebyexperimentid",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<String> deleteByExperimentId(@RequestBody String data, HttpServletRequest request){
        /**
         * 解析参数
         */
        JSONObject jsonObject = JSONObject.parseObject(data);
        Integer experimentId = jsonObject.getInteger("experimentId");
        return experimentService.deleteByExperimentId(experimentId);
    }

    /**
     * 查看实验报告列表
     * @param experimentReportVo
     * @param request
     * @return
     */
    @PostMapping("/getReport")
    public ServerResponse getReport(@RequestBody ExperimentReportVo experimentReportVo, HttpServletRequest request){
        Integer userId = UserUtil.getUserId(request);
        Integer roleId = UserUtil.getRoleId(request);
        try{
            //判断不是管理员和教师身份
            if (!roleId.equals(1) && !roleId.equals(2)) {
                PageHelper.startPage(experimentReportVo.getPageNum(),experimentReportVo.getPageSize());
                List<ExperimentReportVo> allReport = experimentService.getAllReport(userId,experimentReportVo.getMessage());
                PageInfo<ExperimentReportVo> pageInfo = new PageInfo<ExperimentReportVo>(allReport);
                return ServerResponse.createBySuccess("获取实验报告成功",pageInfo);
            }
            PageHelper.startPage(experimentReportVo.getPageNum(),experimentReportVo.getPageSize());
            List<ExperimentReportVo> allReport = experimentService.getAllReport(null,experimentReportVo.getMessage());
            PageInfo<ExperimentReportVo> pageInfo = new PageInfo<ExperimentReportVo>(allReport);
            return ServerResponse.createBySuccess("获取实验报告成功",pageInfo);
        }catch (Exception e){
            return ServerResponse.createByErrorMsg("获取实验报告失败");
        }
    }
}